package com.xxzx.publicprevention.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxzx.publicprevention.mapper.UserMapper;
import com.xxzx.publicprevention.pojo.RecordDO;
import com.xxzx.publicprevention.mapper.RecordMapper;
import com.xxzx.publicprevention.pojo.UserDO;
import com.xxzx.publicprevention.service.RecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liyu
 * @since 2020-11-04
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, RecordDO> implements RecordService {


    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public Integer saveRecord(RecordDO recordDO) {
        recordMapper.addRecord(recordDO);
        return recordDO.getId();
    }

    @Override
    public List<RecordDO> getRecordByUserId(Integer id, Integer beginNum, Integer rowCount, String timeBegin, String timeEnd) {
        List<RecordDO> records = null;
        if (!timeBegin.equals("undefined") && !timeEnd.equals("undefined")) {
            records = recordMapper.getRecordByUserIdWithTime(id, beginNum, rowCount, timeBegin, timeEnd);
        } else {
            records = recordMapper.getRecordByUserId(id, beginNum, rowCount);
        }
        return records;
    }

    @Override
    public List<RecordDO> getRecordByUserIdAndDetail(Integer id, Integer beginNum, Integer rowCount, String detail) {
        List<RecordDO> records = null;
        if (!detail.equals("undefined")) {
            records = recordMapper.getRecordByUserIdWithDetail(id, beginNum, rowCount, detail);
        } else {
            records = recordMapper.getRecordByUserId(id, beginNum, rowCount);
        }
        return records;
    }

    @Override
    public List<RecordDO> getAllRecord() {
        List<RecordDO> records = recordMapper.getAllRecord();
        return records;
    }

    @Override
    public List<RecordDO> getRecordByType(int type) {
        List<RecordDO> records = recordMapper.getRecordByType(type);
        return records;
    }

    @Override
    public void editRecord(RecordDO recordDO) {
        recordMapper.editRecord(recordDO);
        try {
            sentInfoTplMsg(recordMapper.getRecordById(recordDO.getId()));
        } catch (Exception e) {
            System.out.println("推送更新模版消息失败>" + e);
        }
    }

    @Override
    public RecordDO getRecordById(Integer id) {
        RecordDO recordDO = recordMapper.getRecordById(id);
        return recordDO;
    }

    @Override
    public List<RecordDO> getRecordByPage(Integer curr, Integer size) {
        List<RecordDO> records = recordMapper.getRecordByPage(curr, size);
        return records;
    }

    @Override
    public List<RecordDO> getRecordByPageAndType(Integer curr, Integer size, Integer type) {
        List<RecordDO> records = recordMapper.getRecordByPageAndType(curr, size, type);
        return records;
    }

    @Override
    public int getCount() {
        return recordMapper.getCount();
    }



    public void sentInfoTplMsg(RecordDO recordDO) {
        UserDO user = userMapper.getUser(recordDO.getUserId());
        String openID = String.valueOf(user.getOpenId());
        String status = String.valueOf(recordDO.getStatus());
        //todo 日期转str格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String recordCreateTime = sdf.format(recordDO.getCreateTime());
        String recordModifyTime = sdf.format(recordDO.getModifyTime());

        String recordPosition = recordDO.getCity() + recordDO.getDistrict();
        String recordType = String.valueOf(recordDO.getType());
        //todo 发送模版消息推送请求
        String content = "{\"openID\":\"$openID$\",\"status\":\"$status$\",\"recordCreateTime\":\"$recordCreateTime$\",\"recordPosition\":\"$recordPosition$\",\"recordType\":\"$recordType$\",\"recordModifyTime\":\"$recordModifyTime$\"}";
        content = content.replace("$openID$", openID);
        content = content.replace("$status$", status);
        content = content.replace("$recordCreateTime$", recordCreateTime);
        content = content.replace("$recordModifyTime$", recordModifyTime);
        content = content.replace("$recordPosition$", recordPosition);
        content = content.replace("$recordType$", recordType);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> request = new HttpEntity<>(content, headers);
        String url = "http://172.19.2.129/templateMsg/progressUpdated";
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);
        String body = postForEntity.getBody();
        System.out.println(body);
    }
}
